package lagou.mode3;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class FileReadWrite {
    private List<Student> studentList;
    public FileReadWrite(List<Student> studentList){
        this.studentList=studentList;
    }

    public List<Student> fileRead() {
        String pathname = "e:/a.txt";
        File f = new File(pathname);
        if (f.exists()) {
            try {
                FileInputStream filein = new FileInputStream(f);
                ObjectInputStream objin = new ObjectInputStream(filein);
                studentList = (ArrayList<Student>)objin.readObject();
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        return studentList;
    }

    public void fileWrite(){
        ObjectOutputStream objout=null;
        try {
            FileOutputStream fileout;
            fileout = new FileOutputStream("e:/a.txt",true);
            objout = new ObjectOutputStream(fileout);
            objout.writeObject(studentList);
            objout.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            if(objout!=null) {
                try {
                    objout.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
